





var _oErrors =
{
sCannotDeleteDefaultView:		LOCID_MANUIE_CANTDELDEFVIEW,
sNoSelectedView:				LOCID_MANUIE_MUSTSELECT,
sCannotSetDefaultView:			LOCID_MANUIE_CANTSETDEFVIEW
};







var _oConst =
{

entityOtc:						"entityOtc",
uiElementsFilter:				"uiElementsFilter",


sEditAction:					"edit",
sDeleteAction:					"delete",
sSetDefaultAction:				"setDefault",


sCreateViewUrlFormat:			"/tools/vieweditor/viewManager.aspx?objectTypeCode={0}&mode=new&entityId={1}",
sEditViewUrlFormat:				"/tools/vieweditor/viewManager.aspx?id={0}&entityId={1}",
iViewEditorWidth:				800,
iViewEditorHeight:				550,

sPublicView:					"Public",
sDefaultPublicView:				"DefaultPublic",

sSetDefaultUrlFormat:			"/tools/ViewEditor/cmds/cmd_setdefaultview.aspx?newId={0}&oldId={1}&{2}",


sEditFormUrlFormat:				"/tools/formEditor/formEditor.aspx?formtype={0}&objectTypeCode={1}",
sFormEditorWindowNameFormat:	"{0}Editor",
iFormEditorWidth:				750,
iFormEditorHeight:				530
};






var _entityOtc;






var _oUIElementActions =
{

GetAction:			_oUIElementActions_getAction,


form:
{
$edit:			editForm,
$delete:		LOCID_MANUIE_CANTDELFORM,
$setDefault:	LOCID_MANUIE_CANTSETDEFFORM
},

view:
{
$edit:			editView,
$delete:		deleteView,
$setDefault:	setDefaultView
}
}

function _oUIElementActions_getAction(sUIElementType, sAction)
{
return this[sUIElementType]['$' + sAction];
}







function UiElementsJsWindowOnLoad()
{

_entityOtc = gridUIElements.GetParameter(_oConst.entityOtc);


gridUIElements.OnBeforeFormLoad = onUIElementDblClick;
gridUIElements.SetParameter("disableDblClick", "0");
}

window.attachEvent('onload', UiElementsJsWindowOnLoad);










function dispatchAction(sUIElementOid, sAction)
{
if (sUIElementOid == null)
{
alert(_oErrors.sNoSelectedView);
}
else
{
var oAction = _oUIElementActions.GetAction(getUIElementType(sUIElementOid), sAction);

if (typeof(oAction) == "function")
{
oAction(sUIElementOid);
}
else if (typeof(oAction) == "string")
{
alert(oAction);
}
}
}




function createView()
{
var sUrl = prependOrgName(formatString(_oConst.sCreateViewUrlFormat, _entityOtc, GetEntityIdFromQuery()));
openStdWin(sUrl, buildWinName(), _oConst.iViewEditorWidth, _oConst.iViewEditorHeight);
}

function deleteView(sUIElementOid)
{

if (getViewType(sUIElementOid) == _oConst.sDefaultPublicView)
{
alert(_oErrors.sCannotDeleteDefaultView);
return;
}


doAction(null, SavedQuery, _oConst.sDeleteAction, getViewId(sUIElementOid));
gridUIElements.Refresh();
}

function editView(sUIElementOid)
{
if (_bCanUpdateViews)
{
var viewId = getViewId(sUIElementOid);

var sUrl = prependOrgName(formatString(_oConst.sEditViewUrlFormat, viewId, GetEntityIdFromQuery()));
if (viewId == getDefaultViewId())
{
sUrl += "&default=1";
}
openStdWin(sUrl, buildWinName(viewId), _oConst.iViewEditorWidth, _oConst.iViewEditorHeight);
}
else
{
showError(_oSCUErrorCodes.sAccessDenied);
}
}

function setDefaultView(sUIElementOid)
{
var sViewType = getViewType(sUIElementOid);

if (sViewType == _oConst.sPublicView)
{
var sUrl = prependOrgName(formatString(_oConst.sSetDefaultUrlFormat, getViewId(sUIElementOid), getDefaultViewId(), GetTokenAsQS(prependOrgName("/tools/ViewEditor/cmds/cmd_setdefaultview.aspx"))));
var xmlhttp = CreateXmlHttp();
xmlhttp.open("POST", sUrl , false);
xmlhttp.send();
handleXMLErr(xmlhttp.responseXML);

gridUIElements.Refresh();
}
else if (sViewType != _oConst.sDefaultPublicView)
{
alert(_oErrors.sCannotSetDefaultView);
}
}

function getDefaultViewId()
{
var sUIElementOid;
var aRecords = gridUIElements.InnerGrid.AllRecords;

for (var i = 0; i < aRecords.length; i++)
{
sUIElementOid = aRecords[i][0];
if (getViewType(sUIElementOid) == _oConst.sDefaultPublicView)
{
return getViewId(sUIElementOid);
}
}

return "";
}



function getViewType(sUIElementOid)
{
return sUIElementOid.split(":")[1];
}

function getViewId(sUIElementOid)
{
return sUIElementOid.split(":")[2];
}




function editForm(sFormId)
{
if (_bCanUpdateForms)
{
var sUrl = prependOrgName(formatString(_oConst.sEditFormUrlFormat, getFormType(sFormId), _entityOtc));
var sWindowName = formatString(_oConst.sFormEditorWindowNameFormat, getFormType(sFormId));
openStdWin(sUrl, sWindowName, _oConst.iFormEditorWidth, _oConst.iFormEditorHeight);
}
else
{
showError(_oSCUErrorCodes.sAccessDenied);
}
}



function getFormType(sUIElementOid)
{
return sUIElementOid.split(":")[1];
}






function onUIElementDblClick()
{
var sUIElementOid = event.ObjectID;

dispatchAction(sUIElementOid, _oConst.sEditAction);

event.returnValue = false;
}

function onFilterChange()
{
gridUIElements.SetParameter(_oConst.uiElementsFilter, selFilter.DataValue);
gridUIElements.Refresh();
}






function getSelectedUIElementOid()
{
var aRecords = gridUIElements.InnerGrid.SelectedRecords;

return (aRecords.length > 0) ? aRecords[0][0] : null;
}

function getUIElementType(sUIElementOid)
{
return sUIElementOid.split(":")[0];
}

function GetEntityIdFromQuery()
{

var queryString = window.location.search.substring(1);
var pairs = queryString.split("&");
var entityId = "";
for (var i = 0; i < pairs.length; i++)
{
var position = pairs[i].indexOf('=');
if (position >= 0)
{
if(pairs[i].substring(0,position) == "entityId")
{
entityId = pairs[i].substring(position + 1);
}
}
}

return entityId;
}

